<?php

namespace IDock\Insurance\CHAC\Request;

use IDock\Http\RequestFormat;
use IDock\Insurance\CHAC\CHACEnums;
use IDock\Insurance\CHAC\Response\EUnderwritingResponse;
use IDock\Insurance\CHAC\Tools\XMLTools;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\Premium\PremiumCoverageVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class EUnderwritingRequest extends AllCHACRequest
{
    protected $route = 'cxf/policySaveSerivce?wsdl';

    protected $response = EUnderwritingResponse::class;

    protected $urlTip = '提交核保';

    protected $format = RequestFormat::RAW;

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {

        $data = $this->visitor->getExtInfoVisitor()->argc['underwritingData'];

        $data['Head']['queryId'] = $this->getRequestId();
        $data['Head']['requestType'] = '00002';
        $data['BasePart']['orderNumber'] = $this->visitor->getUnderwritingVisitor()->inquiryNo;

        //非车
        $this->noCar($data);

        //保费
        $this->risk($data);

        //险种
        $this->coverage($data);

        unset($data['Head']['orderNumber']);
        unset($data['CarShipTax']);
        unset($data['ChargingPileList']);

        return $data;
    }

    public function soapDataBuild($data): string
    {

        $data = XMLTools::ToXML($data, 'arg0');

        return '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://policySave.provider.app.vhlthirdparty.echannel.ebiz.isoftstone.com/"><soapenv:Header/><soapenv:Body><pol:savePolicy>' . $data . '</pol:savePolicy></soapenv:Body></soapenv:Envelope>';
    }

    /**
     * 非车信息
     * @param $data
     * @return void
     */
    private function noCar(&$data)
    {
        if (!$this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()) {
            return;
        }

        /*** @var $nonCarProductVisitor NonCarProductVisitor */
        $nonCarProductVisitor = $this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()[0];

        $data['BasePart']['nvhlSuitCode'] = $nonCarProductVisitor->planCode;
        $data['BasePart']['payAmtnvhl'] = $nonCarProductVisitor->premium;
    }

    /**
     * 保费
     * @param $data
     * @return void
     */
    private function risk(&$data)
    {
        $riskList = $data['RiskList']['Risk'] ?? [];

        foreach ($riskList as &$item) {
            //交强
            if ($item['riskCode'] == '0801') {
                $item['sumGrossPremium'] = (string)$this->visitor->ciPremium;
            }

            //商业
            if ($item['riskCode'] == '0808') {
                $item['sumGrossPremium'] = (string)$this->visitor->biPremium;
            }
        }

        $data['RiskList']['Risk'] = $riskList;
    }

    /**
     * 险种
     * @param $data
     * @return void
     */
    private function coverage(&$data)
    {

        $kindList = $data['KindList']['Kind'] ?? [];
        if (!$kindList) {
            return;
        }

        $kindList = array_combine(array_column($kindList, 'kindCode'), $kindList);

        $coverageList = $this->visitor->coverageVisitorList;

        /*** @var $item PremiumCoverageVisitor */
        foreach ($coverageList as $item) {
            $coverageCode = CHACEnums::$coverageCode[$item->coverageCode];
            $kindList[$coverageCode]['grossPremium'] = (string)$item->premium;
        }

        $data['KindList']['Kind'] = array_values($kindList);
    }
}
